﻿using UnityEngine;
using YanhuaMMO;
using System.Collections.Generic;
using UnityEngine.UI;
using System;

public class AppView : View {
    private string message;

    private Text showText;
    private Slider progress;
    private float totalCount = 0;
    private float curCount = 0;
    ///<summary>
    /// 监听的消息
    ///</summary>
    List<string> MessageList {
        get {
            return new List<string>()
            {
                NotiConst.UPDATE_MESSAGE,
                NotiConst.UPDATE_EXTRACT,
                NotiConst.UPDATE_DOWNLOAD,
                NotiConst.UPDATE_PROGRESS,
                NotiConst.UPDATE_COUNT,
            };
        }
    }

    void Awake() {
        RemoveMessage(this, MessageList);
        RegisterMessage(this, MessageList);
        showText = transform.Find("bottom/ShowText").GetComponent<Text>();
        progress = transform.Find("bottom/Progress").GetComponent<Slider>();
    }

    /// <summary>
    /// 处理View消息
    /// </summary>
    /// <param name="message"></param>
    public override void OnMessage(IMessage message) {
        string name = message.Name;
        object body = message.Body;
        switch (name) {
            case NotiConst.UPDATE_MESSAGE:      //更新消息
                UpdateMessage(body.ToString());
            break;
            case NotiConst.UPDATE_EXTRACT:      //更新解压
                UpdateExtract(body.ToString());
            break;
            case NotiConst.UPDATE_DOWNLOAD:     //更新下载
                UpdateDownload(body.ToString());
            break;
            case NotiConst.UPDATE_PROGRESS:     //更新下载进度
                UpdateProgress(body.ToString());
            break;
            case NotiConst.UPDATE_COUNT:
                curCount = 0;
                totalCount = Convert.ToSingle(body.ToString());
                progress.gameObject.SetActive(true);
            break;
        }
    }

    public void UpdateMessage(string data) {
        this.message = data;
        string[] strs = data.Split('_');
        if (strs != null && strs.Length > 0)
        {
            showText.text = strs[0];
            if (strs.Length > 1)
            {
                curCount++;
                float downMB = Convert.ToSingle(strs[strs.Length - 2]);
                float totalMB = Convert.ToSingle(strs[strs.Length - 1]);
                if (downMB >= 0 && totalMB > 0 && totalCount > 0)
                {
                    float value = downMB / totalMB;
                    progress.value = value;
                    //float v = curCount / totalCount;
                    //progress.value = v;
                    //if(curCount >= totalCount)
                    //    showText.text = "更新完成";
                    //else
                    //    showText.text = "更新中";
                }
            }         
        }
    }

    public void UpdateExtract(string data) {
        this.message = data;
        showText.text = data;
    }

    public void UpdateDownload(string data) {
        this.message = data;
    }

    public void UpdateProgress(string data) {
        this.message = data;
    }

    //void OnGUI() {
    //    GUI.Label(new Rect(10, 120, 960, 50), message);

    //    GUI.Label(new Rect(10, 0, 500, 50), "(1) 单击 \"Lua/Gen Lua Wrap Files\"。");
    //    GUI.Label(new Rect(10, 20, 500, 50), "(2) 运行Unity游戏");
    //    GUI.Label(new Rect(10, 40, 500, 50), "PS: 清除缓存，单击\"Lua/Clear LuaBinder File + Wrap Files\"。");
    //    GUI.Label(new Rect(10, 60, 900, 50), "PS: 若运行到真机，请设置Const.DebugMode=false，本地调试请设置Const.DebugMode=true");
    //    GUI.Label(new Rect(10, 80, 500, 50), "PS: 加Unity+ulua技术讨论群：>>341746602");
    //}
}
